#include "../func.h"

int main(int argc, char** argv)
{
    if (argc != 3)
    {
        printf("error args \n");
        return -1;
    }
    int fdr, fdw;
    fdr = open(argv[1], O_RDONLY);
    if (-1 == fdr)
    {
        perror("open1");
        return -1;
    }
    fdw = open(argv[2], O_WRONLY);
    if (-1 == fdw)
    {
        perror("open2");
        return -1;
    }
    printf("read fdr = %d, fdw = %d\n", fdr, fdw);
    char buf[20] = {0};
    int ret;
    fd_set rdset;
    struct timeval t;
    t.tv_usec = 0;
    while (1)
    {
        FD_ZERO(&rdset);
        FD_SET(0, &rdset);
        FD_SET(fdr, &rdset);
        t.tv_sec = 3;
        ret = select(fdr + 1, &rdset, NULL, NULL, &t);
        if (ret > 0)
        {
            if (FD_ISSET(0, &rdset))
            {
                memset(buf, 0, sizeof(buf));
                ret = read(0, buf, sizeof(buf));
                if (ret > 0)
                {
                    write(fdw, buf, strlen(buf) - 1);
                }
                else
                {
                    printf("byebye\n");
                    break;
                }
            }
            if (FD_ISSET(fdr, &rdset))
            {
                memset(buf, 0, sizeof(buf));
                ret = read(fdr, buf, sizeof(buf));
                if (ret > 0)
                {
                    printf("B:%s\n", buf);
                }
                else
                {
                    printf("byebye\n");
                    break;
                }
            }
        }
        else
        {
            printf("no fd");
        }
    }

    close(fdr);
    close(fdw);
    return 0;
}
